<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
    function removeEmoji($nickname) {
        $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $nickname);

        return $str;
    }


    function cartesian($arr,$str = array()){
        //去除第一个元素
        $first = array_shift($arr);
        //判断是否是第一次进行拼接
        if(count($str) > 1) {
            foreach ($str as $k => $val) {
                foreach ($first as $key => $value) {
                    //最终实现的格式 1,3,76
                    //可根据具体需求进行变更
                    $str2[] = $val.','.$value;
                }
            }
        }else{
            foreach ($first as $key => $value) {
                //最终实现的格式 1,3,76
                //可根据具体需求进行变更
                $str2[] = $value;
            }
        }

        //递归进行拼接
        if(count($arr) > 0){
            $str2 = cartesian($arr,$str2);
        }
        //返回最终笛卡尔积
        return $str2;
    }
    function getDisField($lat1, $lng1, $lat2, $lng2){
        return "ROUND(6378.138 * 2 * ASIN(SQRT(POW(sin(({$lat1}*pi()/180-{$lat2}*pi()/180)/2),2)+cos({$lat1}*pi()/180)*cos({$lat2}*pi()/180)*pow(sin( ({$lng1}*pi()/180-{$lng2}*pi()/180)/2),2)))*1000) as distance";
    }
    function GetDistance($lat1, $lng1, $lat2, $lng2){
        $pi = 3.1415926535898;
        $rad = 6378.137;
        $radLat1 = $lat1 * ($pi / 180);
        $radLat2 = $lat2 * ($pi / 180);

        $a = $radLat1 - $radLat2;
        $b = ($lng1 * ($pi / 180)) - ($lng2 * ($pi / 180));

        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
        $s = $s * $rad;
        $s = round($s * 10000) / 10000;
        return $s*1000;
    }
// 应用公共文件
    /*
 * 处理订单号，防止测试单号重复
 * @param str $orderid 订单号
 * @param int $len 增加长度
 */
    function proOrder($orderid, $len)
    {
        $chars = array(
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        );
        $charsLen = count($chars) - 1;
        shuffle($chars);                            //打乱数组顺序
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $charsLen)];    //随机取出一位
        }
        return $orderid . $str;
    }
    function count_pages($url) {
        return 1;
        switch (strrchr($url, '.')){
            case '.pdf':
                return get_num_pages_pdf($url);
                break;
            case '.docx':
                $url = str_replace('.docx', '.pdf', $url);
                return get_num_pages_pdf($url);
                break;
            case '.doc':
                $url = str_replace('.doc', '.pdf', $url);
                return get_num_pages_pdf($url);
                break;
            default:
                return 1;
                break;
        }
    }

    function get_num_pages_pdf($filename){
        $image = new Imagick();
        $image->pingImage($filename);
        return $image->getNumberImages();
    }
    function get_num_pages_docx($filename)
    {
        $zip = new ZipArchive();

        if($zip->open($filename) === true)
        {
            if(($index = $zip->locateName('docProps/app.xml')) !== false)
            {
                $data = $zip->getFromIndex($index);
                $zip->close();

                $xml = new SimpleXMLElement($data);
                $arr = (array) $xml;
                return $arr['Pages'];
            }

            $zip->close();
        }

        return false;
    }
    function deleteZip(){
        $handle = @opendir('./');
        while (false !== ($file = readdir($handle))) {//删除当前目录下所有zip文件
            if(is_dir($file)) continue;
            if( preg_match('/.zip/', $file) ) unlink($file);
        }
    }
    //删除文件夹和目录
    function deleteDir($dir){
        if (!$handle = @opendir($dir)) {
            return false;
        }
        while (false !== ($file = readdir($handle))) {
            if ($file !== "." && $file !== "..") {       //排除当前目录与父级目录
                $file = $dir . '/' . $file;
                if (is_dir($file)) {
                    deleteDir($file);
                } else {
                    @unlink($file);
                }
            }

        }
        @rmdir($dir);
    }
    function p($data)
    {
        // 定义样式
        $str = '<pre style="display: block;padding: 9.5px;margin: 44px 0 0 0;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">';
        // 如果是boolean或者null直接显示文字；否则print
        if (is_bool($data)) {
            $show_data = $data ? 'true' : 'false';
        } elseif (is_null($data)) {
            $show_data = 'null';
        } else {
            $show_data = print_r($data, true);
        }
        $str .= $show_data;
        $str .= '</pre>';
        echo $str;
        die;
    }
/*
 * 将下标数组转为关联数组
 * $oldArray:二维数组
 */
if (!function_exists('array_index_by_value')) {
    function array_index_by_value($oldArray = array())
    {
        $newArray = array();
        foreach ($oldArray as $item) {
            $key = array_keys($item[0])[0];
            array_unshift($item, array($key => '全部'));
            $newArray[$key] = $item;
        }
        return $newArray;
    }
}


//支持get，post，delete
if (!function_exists('toIndexArr')) {
    //传入一个关联数组  返回一个索引数组
    function toIndexArr($arr, $keyIndex)
    {
        $newArr = array();
        $i = 0;
        foreach ($arr as $key => $value) {
            $newArr[$i] = $value[$keyIndex];
            $i++;
        }
        return $newArr;
    }

}